package com.shaoshaohuo.app.ui.ec;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.UserInfoEntity;
import com.shaoshaohuo.app.entity.brandscope.BrandScope;
import com.shaoshaohuo.app.framework.AccountManager;
import com.shaoshaohuo.app.listener.UserInfoListener;
import com.shaoshaohuo.app.net.HttpRequest;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.utils.StringUtil;
import com.shaoshaohuo.app.utils.TimeUtil;
import com.shaoshaohuo.app.utils.city.Cityinfo;
import com.shaoshaohuo.app.view.MessageDialog;
import com.shaoshaohuo.app.view.TimepickerPopwindow;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.citymenu.CascadingMenuPopWindow;
import com.shaoshaohuo.app.view.citymenu.entity.Area;
import com.shaoshaohuo.app.view.citymenu.interfaces.CascadingMenuViewOnSelectListener;

/**
 * 提交资料，申请品牌直销采购权限
 * 提交资料后，更新用户资料到最新状态
 * Created by osborn on 16/7/5.
 */
public class BrandScopeApplyActivity extends BaseActivity {
    private TopbarView mTopbarView;
    private TextView etReceiveTime;
    private EditText etName;
    private EditText etShopName;
    private TextView etCity;
    private EditText etDetailAddress;
    private boolean isTimeEdited = false;
    private boolean isCityEdited = false;
    private Cityinfo mCity;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brand_scope_apply);

        mTopbarView = (TopbarView) findViewById(R.id.topbar);
        mTopbarView.setCenterText("品牌直营准入申请");
        mTopbarView.setRightText("提交");
        mTopbarView.setRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commitForm();
            }
        });
        etReceiveTime = (TextView) findViewById(R.id.et_receive_time);
        etName = (EditText) findViewById(R.id.et_name);
        etShopName = (EditText) findViewById(R.id.et_shop_name);
        etCity = (TextView) findViewById(R.id.et_city);
        etCity.setClickable(true);
        etDetailAddress = (EditText) findViewById(R.id.et_detail_address);

        initListeners();
    }

    /**
     * 提交表单
     */
    private void commitForm() {
        if (valideForm()) {
            String name = etName.getText().toString();
            String shopName = etShopName.getText().toString();
            String cityName = etCity.getText().toString();
            String detailAddress = etDetailAddress.getText().toString();
            String receiveTime = etReceiveTime.getText().toString();
            BrandScope brandScope = new BrandScope();
            brandScope.setShopName(shopName);
            brandScope.setReceiveGoodsTime(receiveTime);
            brandScope.setName(name);
            brandScope.setCity(cityName);
            brandScope.setDetailAddress(detailAddress);
            startLoadingDialog();
            RequestService.getInstance().brandScopeApply(this, brandScope, BaseEntity.class, new HttpRequest.RequestListener() {
                @Override
                public void onSuccess(int requestCode, BaseEntity resultData) {
                    if (resultData.isOk()) {
                        AccountManager.updateUserInfo(BrandScopeApplyActivity.this, new UserInfoListener() {
                            @Override
                            public void onUpdateUserInfo(UserInfoEntity userInfoEntity) {
                                dismissLoadingDialog();
                                final MessageDialog dialog = new MessageDialog(BrandScopeApplyActivity.this);
                                dialog.setMessage("准入申请资料提交成功，请等待审核结果。");
                                dialog.setLeftBtVisible(false);
                                dialog.setOnBtClickListener(new MessageDialog.OnBtClickListener() {
                                    @Override
                                    public void onLeftBtClick() {

                                    }

                                    @Override
                                    public void onRightBtClick() {
                                        BrandScopeApplyActivity.this.finish();
                                        dialog.dismiss();
                                    }
                                });
                                dialog.show();
                            }
                        });
                    }
                }

                @Override
                public void onFailed(int requestCode, Exception error, String msg) {

                }
            });
        }
    }

    /**
     * 校验表单
     * @return
     */
    private boolean valideForm() {
        String name = etName.getText().toString();
        if (StringUtil.isEmpty(name)) {
            showToast("请输入真实姓名");
            return false;
        }
        String shopName = etShopName.getText().toString();
        if (StringUtil.isEmpty(shopName)) {
            showToast("请输入店铺名称");
            return false;
        }
        String cityName = etCity.getText().toString();
        if (!isCityEdited || StringUtil.isEmpty(cityName)) {
            showToast("请选择所在市区");
            return false;
        }
        String detailAddress = etDetailAddress.getText().toString();
        if (StringUtil.isEmpty(detailAddress)) {
            showToast("请输入详细地址");
            return false;
        }
        String receiveTime = etReceiveTime.getText().toString();
        if (!isTimeEdited || StringUtil.isEmpty(receiveTime)) {
            showToast("请选择收货时间");
            return false;
        }
        return true;
    }

    private void initListeners() {
        etReceiveTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideSoftInput();
                TimepickerPopwindow timepickerPopwindow = new TimepickerPopwindow(BrandScopeApplyActivity.this);
                timepickerPopwindow.showLocation(etReceiveTime);
                timepickerPopwindow.setOnSelectDateListener(new TimepickerPopwindow.OnSelectDateListener() {
                    @Override
                    public void onSelectDate(int hour, int minute) {
                        isTimeEdited = true;
                        etReceiveTime.setText("" + hour + ":" + TimeUtil.formatMinute(minute));
                    }
                });
            }
        });
        etCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideSoftInput();
                CascadingMenuPopWindow cascadingMenuPopWindowStart = new CascadingMenuPopWindow(BrandScopeApplyActivity.this, false);
                cascadingMenuPopWindowStart
                        .setMenuViewOnSelectListener(new CascadingMenuViewOnSelectListener() {
                            @Override
                            public void getValue(Area city, String fullname) {
                                isCityEdited = true;
//                                mCity = CityinfoUtil.convert(city);
                                if (mCity == null) {
                                    etCity.setText(fullname);
                                } else {
//                                    etCity.setText(AreaUtil.getAreaFullname(city.getAreaId()));
                                }
                            }
                        });
                cascadingMenuPopWindowStart.showAsDropDown(etCity, 5, 5);
            }
        });
    }
}
